package com.biboheart.adapter.core.impl;

import com.biboheart.adapter.core.AdapterService;
import com.biboheart.adapter.core.Collector;
import com.biboheart.adapter.core.Composition;
import com.biboheart.adapter.core.Packager;
import com.biboheart.adapter.model.AdapterConfigModel;
import com.biboheart.adapter.model.StructureNode;
import com.biboheart.adapter.support.utils.JsonUtils;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;
import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * 实现转换服务
 */
public class AdapterServiceImpl implements AdapterService {
    @Override
    public <T> T adapter(Object input, AdapterConfigModel adapterConfigModel, Class<T> clazz) throws JsonProcessingException {
        if (null == input) {
            return null;
        }
        Composition composition = new NormalComposition(adapterConfigModel);
        StructureNode root = composition.getNode("root");
        System.out.println("out structure:" + JsonUtils.obj2json(root));
        Collector collector = new NormalCollector(input);
        Object sourceObject = collector.getObject(null);
        System.out.println("source data hash:" + JsonUtils.obj2json(sourceObject));
        Packager packager = new NormalPackager(composition, collector);
        return ObjectTypeUtils.convertType(packager.result(), clazz);
    }
}
